import java.util.Stack;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/7/9
 * Time:17:01
 **/
public class QuickSort {
    /**
     * 快速排序
     * 时间复杂度：O(nlogn) O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * 调JVM栈参数
     */

    //递归
    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    public static int pivot(int[] array, int start, int end) {
        int tmp = array[start];
        while (start < end) {
            while (start < end && array[end] >= tmp) {
                end--;
            }
            //把数据赋值给start
            array[start] = array[end];
            while (start < end && array[start] <= tmp) {
                start++;
            }
            //把start下标的值给end
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }

    public static void quick(int[] array, int low, int high) {
        //if(low<high){
        if (low >= high) return;
        //进行直接插入法优化
        if (high - low + 1 <= 50) {
            insertSortBound(array, low, high);
            return;//一定要return，说明这个区间范围有序了，直接结束
        }
        //进行三数取中法优化
        medianOfThree(array, low, high);
        int piv = pivot(array, low, high);
        quick(array, low, piv - 1);
        quick(array, piv + 1, high);
        //}
    }


    public static void insertSortBound(int[] array, int low, int high) {
        for (int i = low + 1; i <= high; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= low; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    public static void medianOfThree(int[] array, int low, int high) {
        int mid = (low + high) / 2;
        if (array[low] <= array[mid]) {
            int tmp = array[low];
            array[low] = array[mid];
            array[mid] = tmp;
        }
        if (array[low] >= array[high]) {
            int tmp = array[low];
            array[low] = array[high];
            array[high] = tmp;
        }
        if (array[mid] > array[high]) {
            int tmp = array[mid];
            array[mid] = array[high];
            array[high] = tmp;
        }
    }

    //非递归
    public static void quickSort2(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int low = 0;
        int high = array.length - 1;
        int piv = pivot(array, low, high);
        if (piv > low + 1) {
            stack.push(low);
            stack.push(piv - 1);
        }
        if (piv < high - 1) {
            stack.push(piv + 1);
            stack.push(high);
        }
        while (!stack.empty()) {
            high = stack.pop();
            low = stack.pop();
            piv = pivot(array, low, high);
            if (piv > low + 1) {
                stack.push(low);
                stack.push(piv - 1);
            }
            if (piv < high - 1) {
                stack.push(piv + 1);
                stack.push(high);
            }
        }
    }
}
